package com.sd.repay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sd.repay.channel.cj.CJBusinessImpl;
import com.sd.repay.common.CommonResult;
import com.sd.repay.common.Constants;
import com.sd.repay.pojo.PayOrder;
import com.sd.repay.pojo.RepayPlan;
import com.sd.repay.pojo.RepayPlanDetail;
import com.sd.repay.pojo.ZqMerchant;
import com.sd.repay.service.*;
import com.sd.repay.utils.DES3Coder;
import com.sd.repay.utils.JsonUtil;
import com.sd.repay.utils.StringUtil;
import com.sd.repay.utils.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sd
 * @Description: 畅捷快捷支付Service实现
 * @date 2018年08月19日
 */
@Service
public class CjQuickPayServiceImpl implements QuickPayService {

    private static Logger log = LoggerFactory.getLogger(CjQuickPayServiceImpl.class);

    @Autowired
    private MerTokenService tokenService;
    @Autowired
    private AroundPayOrderService aroundPayOrderService;
    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private RepayPlanDetailService planDetailService;
    @Autowired
    private RepayPlanService planService;
    @Autowired
    private CardManageService cardManageService;
    @Autowired
    private ZqMerchantService zqMerchantService;


    /**
     * 第一次付款
     *
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult payFirst(Map<String, String> map) throws Exception {

        //数据接收
        String batchNo = map.get("batchNo");
        String merchantNo = map.get("merchantNo");
        String cardNo = map.get("cardNo");
        String payAmount = map.get("payAmount");
        String cvn2 = map.get("cvn2");
        String expDate = map.get("expDate");

        RepayPlan repayPlan = planService.getRepayPlanByBatchNo(batchNo);
        log.info("商户编号[{}]第一次付款计划批次号[{}]对应的计划信息[{}]", new Object[]{merchantNo, batchNo, JsonUtil.objectToJson(repayPlan)});
        RepayPlanDetail planDetail = planDetailService.getFirstDetail(batchNo);
        log.info("商户编号[{}]第一次付款计划批次号[{}]对应的第一笔流水信息[{}]", new Object[]{merchantNo, batchNo, JsonUtil.objectToJson(planDetail)});

        //根据卡片编号获取卡片对象
        Map<String, Object> cardInfo = cardManageService.getCardManageByCardNo(cardNo);

        String mobileNo = StringUtil.filterNull(cardInfo.get("mobile_no"));
        String settleBankNo = StringUtil.filterNull(cardInfo.get("settle_bank_no"));
        String idCardNo = StringUtil.filterNull(cardInfo.get("id_card_no"));
        String accountName = StringUtil.filterNull(cardInfo.get("account_name"));
        String accountNo = StringUtil.filterNull(cardInfo.get("account_no"));

        //获取进件商户号
        ZqMerchant zqMerchant = zqMerchantService.getByMerAndAcq(merchantNo, Constants.PAY_ACQ_CODE_CJ);
        log.info("畅捷生成还款计划批次号" + batchNo + "对应商户" + merchantNo + "上游商户信息：" + JsonUtil.objectToJson(zqMerchant));
        if (null == zqMerchant) {
            return CommonResult.build(403, "商户未进件或进件失败，请重新建立计划");
        }
        String zqMerchantNo = zqMerchant.getZqMerchantNo();
        String zqStatus = zqMerchant.getZqStatus();
        if (StringUtils.isEmpty(zqMerchantNo) || !Constants.ZQ_MER_STATUS_SUCC.equals(zqStatus)) {
            return CommonResult.build(403, "商户未进件或进件失败，请重新建立计划");
        }
        //判断支付卡片是否已绑卡，已绑卡则直接支付，未绑卡则进行绑卡操作
        String cjToken = tokenService.getTokenByMerAndAcqAndCard(merchantNo, cardNo, Constants.PAY_ACQ_CODE_CJ);
        log.info("商户号：" + merchantNo + "，卡片编号：" + cardNo + "数据库对应token：" + cjToken);
        if (StringUtils.isBlank(cjToken)) {

            String smsOrderNo = WebUtil.nextUniqueSeqNo("t_bind_card_record", "CJ_BIND", 25);
            //绑卡短信
            Map<String, String> bindCardSmsReqMap = new HashMap<>();
            bindCardSmsReqMap.put("merchantNo", merchantNo);
            bindCardSmsReqMap.put("cardNo", cardNo);
            bindCardSmsReqMap.put("merchantCode", zqMerchantNo);
            bindCardSmsReqMap.put("orderNo", smsOrderNo);
            bindCardSmsReqMap.put("bankAccountNo", accountNo);
            bindCardSmsReqMap.put("bankAccountName", accountName);
            bindCardSmsReqMap.put("bankNo", settleBankNo);
            bindCardSmsReqMap.put("idCardNo", idCardNo);
            bindCardSmsReqMap.put("mobileNo", mobileNo);
            CommonResult bindCardSmsRes = CJBusinessImpl.merBindCardSignSms(bindCardSmsReqMap);

            if (bindCardSmsRes.getStatus() != 200) {
                return bindCardSmsRes;
            }
            JSONObject bindCardSmsResJson = (JSONObject) bindCardSmsRes.getData();
            String bindRespCode = bindCardSmsResJson.getString("respCode");
            String bindRespMsg = bindCardSmsResJson.getString("respMsg");
            String bindStatus = bindCardSmsResJson.getString("signStatus");
            if (!"000000".equals(bindRespCode)) {
                return CommonResult.build(403, bindRespMsg);
            }
            if ("2".equals(bindStatus)) {

                String cvn2Des3 = DES3Coder.decryptToString(cvn2);
                String expDateDes3 = DES3Coder.decryptToString(expDate);
                cjToken = cvn2Des3.concat("\\+").concat(expDateDes3);
                int count = tokenService.saveTokenInfo(merchantNo, cardNo, Constants.PAY_ACQ_CODE_CJ, cjToken, "", "");
                log.info("商户号[{}]，卡片编号[{}]畅捷绑卡成功保存token[{}]到数据库影响的行数[{}]", new Object[]{merchantNo, cardNo, cjToken, count});
                if (count > 0) {
                    map.put("planNo", planDetail.getPlanNo());
                    return executeQuickPay(map);
                }
                return CommonResult.build(403, "支付失败，请稍候再试");
            }
            //封装返回值
            Map<String, String> resMap = new HashMap<>();
            resMap.put("service", "smsCode");
            resMap.put("serviceOrderNo", smsOrderNo);
            resMap.put("payOrderNo", "");
            resMap.put("batchNo", batchNo);
            resMap.put("cardNo", cardNo);
            resMap.put("payAmount", payAmount);
            resMap.put("cvn2", DES3Coder.encryptToString(cvn2));
            resMap.put("expDate", DES3Coder.encryptToString(expDate));
            return CommonResult.ok(resMap);
        }
        map.put("planNo", planDetail.getPlanNo());
        return executeQuickPay(map);
    }

    /**
     * 第一次确认付款（用户下发短信验证码操作的）
     *
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult confirmPayFirst(Map<String, String> params) throws Exception {

        //数据接收
        String service = params.get("service");
        String serviceOrderNo = params.get("serviceOrderNo");
        String smsCode = params.get("serviceValue");
        String payOrderNo = params.get("payOrderNo");
        String merchantNo = params.get("merchantNo");
        String batchNo = params.get("batchNo");
        String cardNo = params.get("cardNo");
        String payAmount = params.get("payAmount");
        String cvn2 = params.get("cvn2");
        String expDate = params.get("expDate");

        RepayPlan repayPlan = planService.getRepayPlanByBatchNo(batchNo);
        log.info("商户编号[{}]第一次付款计划批次号[{}]对应的计划信息[{}]", new Object[]{merchantNo, batchNo, JsonUtil.objectToJson(repayPlan)});
        RepayPlanDetail planDetail = planDetailService.getFirstDetail(batchNo);
        log.info("商户编号[{}]第一次付款计划批次号[{}]对应的第一笔流水信息[{}]", new Object[]{merchantNo, batchNo, JsonUtil.objectToJson(planDetail)});

        //根据卡片编号获取卡片对象
        Map<String, Object> cardInfo = cardManageService.getCardManageByCardNo(cardNo);

        String mobileNo = StringUtil.filterNull(cardInfo.get("mobile_no"));
        String settleBankNo = StringUtil.filterNull(cardInfo.get("settle_bank_no"));
        String idCardNo = StringUtil.filterNull(cardInfo.get("id_card_no"));
        String accountName = StringUtil.filterNull(cardInfo.get("account_name"));
        String accountNo = StringUtil.filterNull(cardInfo.get("account_no"));
        String bankName = StringUtil.filterNull(cardInfo.get("bank_name"));

        //获取进件商户号
        ZqMerchant zqMerchant = zqMerchantService.getByMerAndAcq(merchantNo, Constants.PAY_ACQ_CODE_CJ);
        log.info("畅捷生成还款计划批次号" + batchNo + "对应商户" + merchantNo + "上游商户信息：" + JsonUtil.objectToJson(zqMerchant));
        if (null == zqMerchant) {
            return CommonResult.build(403, "商户未进件或进件失败，请重新建立计划");
        }
        String zqMerchantNo = zqMerchant.getZqMerchantNo();
        String zqStatus = zqMerchant.getZqStatus();
        if (StringUtils.isEmpty(zqMerchantNo) || !Constants.ZQ_MER_STATUS_SUCC.equals(zqStatus)) {
            return CommonResult.build(403, "商户未进件或进件失败，请重新建立计划");
        }

        //畅捷绑卡
        Map<String, String> bindCardReqMap = new HashMap<>();
        bindCardReqMap.put("merchantNo", merchantNo);
        bindCardReqMap.put("cardNo", cardNo);
        bindCardReqMap.put("merchantCode", zqMerchantNo);
        bindCardReqMap.put("orderNo", serviceOrderNo);
        bindCardReqMap.put("bankAccountNo", accountNo);
        bindCardReqMap.put("bankAccountName", accountName);
        bindCardReqMap.put("bankNo", settleBankNo);
        bindCardReqMap.put("idCardNo", idCardNo);
        bindCardReqMap.put("mobileNo", mobileNo);
        bindCardReqMap.put("cvn2", cvn2);
        bindCardReqMap.put("expired", expDate);
        bindCardReqMap.put("smsCode", smsCode);

        CommonResult bindCardRes = CJBusinessImpl.merBindCardSign(bindCardReqMap);

        if (bindCardRes.getStatus() != 200) {
            return bindCardRes;
        }
        JSONObject bindCardSmsResJson = (JSONObject) bindCardRes.getData();
        String bindRespCode = bindCardSmsResJson.getString("respCode");
        String bindRespMsg = bindCardSmsResJson.getString("respMsg");
        String bindStatus = bindCardSmsResJson.getString("signStatus");
        if (!"000000".equals(bindRespCode)) {
            return CommonResult.build(403, bindRespMsg);
        }
        if (!"2".equals(bindStatus)) {
            return CommonResult.build(403, bindRespMsg);
        }

        String cvn2Des3 = DES3Coder.encryptToString(cvn2);
        String expDateDes3 = DES3Coder.encryptToString(expDate);
        String cjToken = cvn2Des3.concat("&&").concat(expDateDes3);
        String oldcjToken = tokenService.getTokenByMerAndAcqAndCard(merchantNo, cardNo, Constants.PAY_ACQ_CODE_CJ);
        int count;
        if (StringUtils.isNotBlank(oldcjToken)) {
            count = tokenService.updateToken(merchantNo, cardNo, Constants.PAY_ACQ_CODE_CJ, cjToken);
        } else {
            count = tokenService.saveTokenInfo(merchantNo, cardNo, Constants.PAY_ACQ_CODE_CJ, cjToken, "", "");
        }
        log.info("商户号[{}]，卡片编号[{}]畅捷绑卡成功保存token[{}]到数据库影响的行数[{}]", new Object[]{merchantNo, cardNo, cjToken, count});
        params.put("planNo", planDetail.getPlanNo());

        return executeQuickPay(params);
    }

    /**
     * 消费明细付款
     *
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult executeQuickPay(Map<String, String> params) throws Exception {

        log.info("畅捷快捷消费参数：[{}]", JsonUtil.objectToJson(params));

        //数据接收
        String planNo = params.get("planNo");
        String merchantNo = params.get("merchantNo");
        String cardNo = params.get("cardNo");
        String payAmount = params.get("payAmount");

        if (StringUtil.isBlank(planNo, merchantNo, cardNo, payAmount)) {
            return CommonResult.build(403, "参数不能有空值");
        }

        //获取消费明细
        RepayPlanDetail planDetail = planDetailService.getDetailByPlanNo(planNo);
        int planIndex = planDetail.getPlanIndex();
        String batchNo = planDetail.getBatchNo();
        log.info("计划明细编号：[{}]对应明细对象：[{}]", new Object[]{planNo, JsonUtil.objectToJson(planDetail)});
        if (planDetail == null || !Constants.PLAN_DETAIL_TYPE_OUT.equals(planDetail.getPlanType())) {
            return CommonResult.build(403, "数据不合法");
        }

        //根据卡片编号获取卡片对象
        Map<String, Object> cardInfo = cardManageService.getCardManageByCardNo(cardNo);

        String mobileNo = StringUtil.filterNull(cardInfo.get("mobile_no"));
        String settleBankNo = StringUtil.filterNull(cardInfo.get("settle_bank_no"));
        String idCardNo = StringUtil.filterNull(cardInfo.get("id_card_no"));
        String accountName = StringUtil.filterNull(cardInfo.get("account_name"));
        String accountNo = StringUtil.filterNull(cardInfo.get("account_no"));
        String bankName = StringUtil.filterNull(cardInfo.get("bank_name"));

        //获取进件商户号
        ZqMerchant zqMerchant = zqMerchantService.getByMerAndAcq(merchantNo, Constants.PAY_ACQ_CODE_CJ);
        log.info("畅捷计划明细编号" + planNo + "对应商户" + merchantNo + "上游商户信息：" + JsonUtil.objectToJson(zqMerchant));
        if (null == zqMerchant) {
            return CommonResult.build(403, "商户未进件或进件失败，请重新建立计划");
        }
        String zqMerchantNo = zqMerchant.getZqMerchantNo();
        String zqStatus = zqMerchant.getZqStatus();
        if (StringUtils.isEmpty(zqMerchantNo) || !Constants.ZQ_MER_STATUS_SUCC.equals(zqStatus)) {
            return CommonResult.build(403, "商户未进件或进件失败，请重新建立计划");
        }

        //判断支付卡片是否已绑卡，已绑卡则直接支付，未绑卡则进行绑卡操作
        String cjToken = tokenService.getTokenByMerAndAcqAndCard(merchantNo, cardNo, Constants.PAY_ACQ_CODE_CJ);
        log.info("商户号：" + merchantNo + "，卡片编号：" + cardNo + "畅捷数据库对应token：" + cjToken);
        if (StringUtils.isBlank(cjToken)) {
            return CommonResult.build(403, "支付卡未绑卡，请重新建立计划");
        }
        String[] tokenArray = cjToken.split("&&");
        if (tokenArray.length != 2) {
            return CommonResult.build(403, "支付卡未绑卡，请重新建立计划");
        }
        String cvn2Des3 = tokenArray[0];
        String expDateDes3 = tokenArray[1];

        String cvn2 = DES3Coder.decryptToString(cvn2Des3);
        String expDate = DES3Coder.decryptToString(expDateDes3);

        //生成交易订单
        String orderNo = "";
        try {
            CommonResult orderRes = aroundPayOrderService.prePayOrder(merchantNo, cardNo, planDetail.getPlanNo(), payAmount,
                    Constants.PAY_ACQ_CODE_CJ);
            if (200 != orderRes.getStatus()) {
                return orderRes;
            }
            Map<String, String> orderResMap = (Map<String, String>) orderRes.getData();
            orderNo = orderResMap.get("orderNo");
        } catch (Exception e) {
            log.info("计划明细编号[{}]支付异常[{}]", new Object[]{planNo, e});
            return CommonResult.build(403, "支付失败，请稍候再试");
        }

        //畅捷快捷消费
        Map<String, String> payReqMap = new HashMap<>();
        payReqMap.put("merchantNo", merchantNo);
        payReqMap.put("cardNo", cardNo);
        payReqMap.put("merchantCode", zqMerchantNo);
        payReqMap.put("orderNo", orderNo);
        payReqMap.put("amount", payAmount);
        payReqMap.put("bankAccountNo", accountNo);
        payReqMap.put("bankAccountName", accountName);
        payReqMap.put("bankNo", settleBankNo);
        payReqMap.put("idCardNo", idCardNo);
        payReqMap.put("mobileNo", mobileNo);
        payReqMap.put("cvn2", cvn2);
        payReqMap.put("expired", expDate);

        Map<String, String> payResMap = new HashMap<>();
        int count;
        try {
            CommonResult payRes = CJBusinessImpl.merConsume(payReqMap);
            log.info("计划明细编号[{}]执行畅捷快捷消费返回信息[{}]", new Object[]{planNo, JsonUtil.objectToJson(payRes)});

            if (planIndex == 1) {
                // 第一笔交易发起之后将批次状态改为执行中
                count = planService.updateRepayPlanStatus(batchNo, Constants.PLAN_STATUS_IN_EXECUTE, Constants.PLAN_STATUS_INIT, "");
                log.info("计划批次号[{}]第一笔明细[{}]发起交易，计划状态改为执行中影响的行数[{}]", new Object[]{batchNo, planNo, count});
            }

            if (payRes.getStatus() == 200) {

                JSONObject payResJson = (JSONObject) payRes.getData();

                String respCode = payResJson.getString("respCode");
                String respMsg = payResJson.getString("respMsg");

                if ("000000".equals(respCode)) {

                    // 回写交易订单相关信息
                    count = payOrderService.writeBackPayOrderInfo(orderNo, "", null,
                            Constants.ORDER_TRANS_STATUS_UNKNOW, Constants.ORDER_TRANS_STATUS_INIT, respCode, respMsg);
                    log.info("执行畅捷快捷消费还款计划编号" + planNo + "接口成功，回写交易订单相关信息：" + orderNo + "状态为未知影响的行数为" + count);

                    if (count > 0 && planIndex == 1) {
                        //修改第一笔明细状态为未知
                        count = planDetailService.updateDetailStatus(planNo, Constants.PLAN_DETAIL_STATUS_IN_EXECUTE, "", respMsg);
                        log.info("计划批次号[{}]第一笔明细[{}]交易未知，明细状态改为未知影响的行数[{}]", new Object[]{batchNo, planNo, count});

                        // 第一笔交易未知，计划也要改为未执行状态
                        count = planService.updateRepayPlanStatus(batchNo, Constants.PLAN_STATUS_UN_EXECUTE, Constants.PLAN_STATUS_IN_EXECUTE, respMsg);
                        log.info("计划批次号[{}]第一笔明细[{}]交易未知，计划明细状态改为未执行状态影响的行数[{}]", new Object[]{batchNo, planNo, count});
                    }

                    payResMap.put("service", "");
                    payResMap.put("orderNo", orderNo);
                    payResMap.put("resCode", respCode);
                    payResMap.put("resMsg", respMsg);
                    payResMap.put("resStatus", Constants.ORDER_TRANS_STATUS_UNKNOW);

                } else {

                    // 回写交易订单相关信息
                    count = payOrderService.writeBackPayOrderInfo(orderNo, "", null,
                            Constants.ORDER_TRANS_STATUS_FAIL, Constants.ORDER_TRANS_STATUS_INIT, respCode, respMsg);
                    log.info("执行畅捷快捷消费还款计划编号" + planNo + "交易失败，回写交易订单相关信息：" + orderNo + "状态为失败影响的行数为" + count);

                    if (count > 0 && planIndex == 1) {
                        //修改第一笔明细状态为失败
                        count = planDetailService.updateDetailStatus(planNo, Constants.PLAN_DETAIL_STATUS_FAIL, "", respMsg);
                        log.info("计划批次号[{}]第一笔明细[{}]交易失败，明细状态改为失败影响的行数[{}]", new Object[]{batchNo, planNo, count});

                        // 第一笔交易失败，计划也要改为失败状态
                        count = planService.updateRepayPlanStatus(batchNo, Constants.PLAN_STATUS_FAIL, Constants.PLAN_STATUS_IN_EXECUTE, respMsg);
                        log.info("计划批次号[{}]第一笔明细[{}]交易失败，计划明细状态改为失败影响的行数[{}]", new Object[]{batchNo, planNo, count});
                    }

                    payResMap.put("service", "");
                    payResMap.put("orderNo", orderNo);
                    payResMap.put("resCode", respCode);
                    payResMap.put("resMsg", respMsg);
                    payResMap.put("resStatus", Constants.ORDER_TRANS_STATUS_FAIL);

                }
            } else {
                log.error("执行畅捷快捷消费还款计划编号" + planNo + "支付接口失败：" + payRes.getMsg() + "，并将此还款明细状态改为未知");
                // 回写交易订单相关信息
                count = payOrderService.writeBackPayOrderInfo(orderNo, "", null,
                        Constants.ORDER_TRANS_STATUS_UNKNOW, Constants.ORDER_TRANS_STATUS_INIT, "", payRes.getMsg());
                log.info("执行畅捷快捷消费还款计划编号" + planNo + "支付接口失败，回写交易订单相关信息：" + orderNo + "状态为未知影响的行数为" + count);

                if (count > 0 && planIndex == 1) {
                    //修改第一笔明细状态为未知
                    count = planDetailService.updateDetailStatus(planNo, Constants.PLAN_DETAIL_STATUS_IN_EXECUTE, "", payRes.getMsg());
                    log.info("计划批次号[{}]第一笔明细[{}]交易未知，明细状态改为未知影响的行数[{}]", new Object[]{batchNo, planNo, count});

                    // 第一笔交易未知，计划也要改为未执行状态
                    count = planService.updateRepayPlanStatus(batchNo, Constants.PLAN_STATUS_UN_EXECUTE, Constants.PLAN_STATUS_IN_EXECUTE, payRes.getMsg());
                    log.info("计划批次号[{}]第一笔明细[{}]交易未知，计划明细状态改为未执行状态影响的行数[{}]", new Object[]{batchNo, planNo, count});
                }

                payResMap.put("service", "");
                payResMap.put("orderNo", orderNo);
                payResMap.put("resCode", "");
                payResMap.put("resMsg", payRes.getMsg());
                payResMap.put("resStatus", Constants.ORDER_TRANS_STATUS_UNKNOW);
            }
        } catch (Exception e) {
            e.printStackTrace();

            log.error("执行畅捷快捷消费还款计划编号" + planNo + "支付异常：" + e.getMessage() + "，并将此还款明细状态改为未知");
            // 回写交易订单相关信息
            count = payOrderService.writeBackPayOrderInfo(orderNo, "", null,
                    Constants.ORDER_TRANS_STATUS_UNKNOW, Constants.ORDER_TRANS_STATUS_INIT, "", "快捷消费异常");
            log.info("执行畅捷快捷消费还款计划编号" + planNo + "支付异常回写交易订单相关信息：" + orderNo + "状态为未知影响的行数为" + count);

            if (count > 0 && planIndex == 1) {
                //修改第一笔明细状态为未知
                count = planDetailService.updateDetailStatus(planNo, Constants.PLAN_DETAIL_STATUS_IN_EXECUTE, "", "快捷消费异常");
                log.info("计划批次号[{}]第一笔明细[{}]交易未知，明细状态改为未知影响的行数[{}]", new Object[]{batchNo, planNo, count});

                // 第一笔交易未知，计划也要改为未执行状态
                count = planService.updateRepayPlanStatus(batchNo, Constants.PLAN_STATUS_UN_EXECUTE, Constants.PLAN_STATUS_IN_EXECUTE, "快捷消费异常");
                log.info("计划批次号[{}]第一笔明细[{}]交易未知，计划明细状态改为未执行状态影响的行数[{}]", new Object[]{batchNo, planNo, count});
            }

            payResMap.put("service", "");
            payResMap.put("orderNo", orderNo);
            payResMap.put("resCode", "");
            payResMap.put("resMsg", "快捷消费异常");
            payResMap.put("resStatus", Constants.ORDER_TRANS_STATUS_UNKNOW);
        }

        return CommonResult.ok(payResMap);
    }

    /**
     * 订单查询
     *
     * @param payOrder
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult queryOrderInfo(PayOrder payOrder) throws Exception {

        String merchantNo = payOrder.getMerchantNo();
        String orderNo = payOrder.getOrderNo();

        //获取进件商户号
        ZqMerchant zqMerchant = zqMerchantService.getByMerAndAcq(merchantNo, Constants.PAY_ACQ_CODE_CJ);
        log.info("畅捷交易订单号" + orderNo + "对应商户" + merchantNo + "上游商户信息：" + JsonUtil.objectToJson(zqMerchant));
        if (null == zqMerchant) {
            return CommonResult.build(403, "商户未进件或进件失败");
        }
        String zqMerchantNo = zqMerchant.getZqMerchantNo();
        String zqStatus = zqMerchant.getZqStatus();
        if (StringUtils.isEmpty(zqMerchantNo) || !Constants.ZQ_MER_STATUS_SUCC.equals(zqStatus)) {
            return CommonResult.build(403, "商户未进件或进件失败");
        }
        Map<String, String> queryMap = new HashMap<>();
        queryMap.put("orderNo", orderNo);
        queryMap.put("merchantNo", merchantNo);
        queryMap.put("merchantCode", zqMerchantNo);
        CommonResult queryRes = CJBusinessImpl.queryOrder(queryMap);

        if (queryRes.getStatus() == 200) {
            JSONObject queryOrderResJson = (JSONObject) queryRes.getData();

            String respCode = queryOrderResJson.getString("respCode");
            String respMsg = queryOrderResJson.getString("respMsg");
            String orderStatus = queryOrderResJson.getString("orderStatus");

            Map<String, String> map = new HashMap<>();

            map.put("acqOrderNo", "");
            map.put("orderNo", orderNo);
            map.put("resCode", respCode);
            map.put("resMsg", respMsg);
            map.put("transTime", "");

            if ("2".equals(orderStatus)) {
                map.put("transStatus", "2");
            } else if ("4".equals(orderStatus) || "5".equals(orderStatus)) {
                map.put("transStatus", "3");
            } else {
                map.put("transStatus", "4");
            }

            return CommonResult.ok(map);
        }
        return queryRes;
    }
}
